package com.spatio.serving.basic.service.template.server.controller;

import com.spatio.serving.basic.service.template.common.domain.AuthorizationDo;
import com.spatio.serving.basic.service.template.common.domain.UserInfoDo;
import com.spatio.serving.basic.service.template.common.domain.UserLoginDo;
import com.spatio.serving.basic.service.template.core.component.service.UserService;
import com.spatio.serving.basic.service.template.mysql.Result.Result;
import com.spatio.serving.basic.service.template.core.component.invoker.createUserDocInvoker;

import com.spatio.serving.basic.service.template.server.Interceptor.JWTInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.math.BigInteger;


@RestController
@RequestMapping("/api/v1/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private createUserDocInvoker createUserDocInvoker;


    @PostMapping("/hello")
    public String test(@RequestParam String name) throws IOException {
        BigInteger user_id = JWTInterceptor.getUserInfo().getUser_id();
        boolean flag = createUserDocInvoker.createOrUpdateDoc(user_id);
        return name;
    }

    /**
     * 获取用户信息
     * @return
     * @throws Exception
     */
    @GetMapping("/user_info")
    public Result getUserInfo() throws Exception {
        UserInfoDo user = new UserInfoDo();
        BigInteger user_id = JWTInterceptor.getUserInfo().getUser_id();
        user.setUser_id(user_id);
        UserInfoDo userInfo = userService.getUserInfo(user);
        return Result.success(userInfo);

    }

    /**
     * 用户输入验证码登陆
     * @param user
     * @return
     * @throws Exception
     */
    @PostMapping("/code")
    public Result sendCode(@RequestBody UserLoginDo user) throws Exception {

        UserLoginDo res = userService.sendCode(user);
            //user=userService.getUserInfo(user);
        return Result.success(res);

    }

    /**
     * 用户登录
     * @param user
     * @return
     */
    @CrossOrigin
    @PostMapping("/login")
    public Result login(@RequestBody UserLoginDo user) {
        AuthorizationDo auth=new AuthorizationDo();
        auth.setAuthorization(userService.login(user));
        if(auth.getAuthorization().equals("登陆失败")){
            return Result.error(auth.getAuthorization());
        }else {
            return Result.success(auth);
        }
        //log.info("{} 请求验证码...",phone);
    }

    /**
     * 用户填写/修改个人信息
     * @param user
     * @return
     */
    @PostMapping("/update_info")
    public Result update_info(@RequestBody UserInfoDo user) {
        //log.info("{} 请求验证码...",phone);
        BigInteger user_id = JWTInterceptor.getUserInfo().getUser_id();
        user.setUser_id(user_id);
        boolean success = userService.update(user);
        if (success) {
            return Result.success("更新信息成功");
        } else {
            return Result.error("更新信息失败");
        }
    }
//        boolean flag = userService.update(user);
//        if(flag){
//            return Result.success("更新信息成功");
//        }
//        return Result.error("更新信息失败，请检查数据类型");
}